home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / CHELP.PAK / CHELP.C next >
C/C++ Source or Header  |  1997-05-06  |  5KB  |  180 lines

  1. // Windows 3.1 Demo Program
  2. // Copyright (c) 1992 by Borland International
  3.  
  4. /* -------------------------------------------------------------
  5.  This is an example of an application which implements context
  6.  sensitive help for menu choices.  To use the application, hit
  7.  F1 when a menu item is highlighted.  The program checks for F1
  8.  being down in the WM_ENTERIDLE message.  If it is down, it sets
  9.  a flag and simulates the selection of the menu item.  The help
  10.  is then shown in the command message for that menu item.
  11.  When the command is received, we just check to see if the flag
  12.  has been set which indicates that the user wants help on the command.
  13.  ---------------------------------------------------------------- */
  14.  
  15. #define STRICT
  16. #include <windows.h>
  17. #pragma hdrstop
  18. #include <windowsx.h>
  19.  
  20. #include "chelpm.h"
  21. #include "chelpr.h"
  22.  
  23. LRESULT FAR PASCAL _export WndProc( HWND, UINT , WPARAM, LPARAM );
  24.  
  25. #pragma argsused
  26. int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.           LPSTR lpszCmd, int nCmdShow)
  28. {
  29.    static char szAppName[] = "CHelp" ;
  30.    HWND        hwnd ;
  31.    MSG         msg ;
  32.    WNDCLASS    wndclass ;
  33.    HANDLE    hAccel;
  34.  
  35.    if (!hPrevInstance)
  36.    {
  37.       wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  38.       wndclass.lpfnWndProc   = (WNDPROC)WndProc ;
  39.       wndclass.cbClsExtra    = 0 ;
  40.       wndclass.cbWndExtra    = 0 ;
  41.       wndclass.hInstance     = hInstance ;
  42.       wndclass.hIcon         = LoadIcon( NULL, IDI_APPLICATION );
  43.       wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  44.       wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  45.       wndclass.lpszMenuName  = MAKEINTRESOURCE( CHELPAPMENU );
  46.       wndclass.lpszClassName = szAppName ;
  47.  
  48.       RegisterClass (&wndclass) ;
  49.   }
  50.  
  51.   hwnd = CreateWindow (szAppName,
  52.     "Windows Help Example",
  53.     WS_OVERLAPPEDWINDOW,
  54.     CW_USEDEFAULT,
  55.     CW_USEDEFAULT,
  56.     CW_USEDEFAULT,
  57.     CW_USEDEFAULT,
  58.     NULL,
  59.     NULL,
  60.     hInstance,
  61.     NULL) ;
  62.  
  63.    ShowWindow (hwnd, nCmdShow) ;
  64.    UpdateWindow (hwnd) ;
  65.  
  66.    hAccel = LoadAccelerators( hInstance,
  67.       MAKEINTRESOURCE( CHELPAPACCEL ) );
  68.  
  69.    while (GetMessage (&msg, NULL, 0, 0))
  70.    {
  71.     if ( !TranslateAccelerator( hwnd, hAccel, &msg ))
  72.     {
  73.        TranslateMessage (&msg) ;
  74.        DispatchMessage (&msg) ;
  75.     }
  76.    }
  77.    return msg.wParam ;
  78. }
  79.  
  80. LPSTR lpszClientAreaText = "To Get help, press F1 while a menu item is \
  81. highlighted.  WinHelp will be called to show help on that topic.  If help \
  82. is not shown, it is because the mouse button is pressed.  Release the \
  83. mouse button to see help.";
  84.  
  85. LRESULT FAR PASCAL _export WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  86. {
  87.    static BOOL tfF1Pressed = FALSE;
  88.  
  89.    switch (message)
  90.    {
  91.       case WM_PAINT:
  92.       {
  93.         HDC hdc;
  94.         PAINTSTRUCT ps;
  95.         RECT rectClient;
  96.         int nWidth, nHeight;
  97.  
  98.         hdc = BeginPaint( hwnd, &ps );
  99.         GetClientRect( hwnd , &rectClient );
  100.  
  101.         nWidth = rectClient.right;    // rectClient.left is 0
  102.         nHeight = rectClient.bottom;  // rectClient.top is 0
  103.  
  104.         rectClient.left += (nWidth / 4);
  105.         rectClient.top  += (nHeight / 4);
  106.         rectClient.right -= (nWidth / 4);
  107.  
  108.         SetBkMode( hdc, TRANSPARENT );
  109.         DrawText( hdc, lpszClientAreaText, lstrlen( lpszClientAreaText ),
  110.           &rectClient, DT_CENTER | DT_VCENTER | DT_WORDBREAK );
  111.         EndPaint( hwnd, &ps );
  112.       }
  113.       break;
  114.  
  115.       case WM_ENTERIDLE:
  116.       {
  117.         if( (wParam == MSGF_MENU ) &&
  118.           ((GetKeyState( VK_F1 ) & 0x8000) != 0) )
  119.           // The key is down if the high order bit is set.
  120.         {
  121.           tfF1Pressed = TRUE;
  122.           PostMessage( hwnd, WM_KEYDOWN, VK_RETURN, 0L );
  123.         }
  124.       }
  125.       break;
  126.       case WM_COMMAND:
  127.       switch ( GET_WM_COMMAND_ID(wParam, lParam) )
  128.       {
  129.         case CM_U_MENUITEMA:
  130.         {
  131.           if( tfF1Pressed == TRUE )
  132.           {
  133.             WinHelp( hwnd, "CHELP.HLP", HELP_CONTEXT, HELP_MENUITEMA );
  134.             tfF1Pressed = FALSE;
  135.           } else {
  136.             MessageBox( hwnd, "In Menu Item A command", "C Help Example", MB_ICONINFORMATION );
  137.           }
  138.         }
  139.         break;
  140.       case  CM_U_MENUITEMB :
  141.       {
  142.         if( tfF1Pressed == TRUE )
  143.         {
  144.           WinHelp( hwnd, "CHELP.HLP", HELP_CONTEXT, HELP_MENUITEMB );
  145.           tfF1Pressed = FALSE;
  146.         } else {
  147.           MessageBox( hwnd, "In Menu Item B Command", "C help Example", MB_ICONINFORMATION );
  148.         }
  149.       }
  150.       break;
  151.       case CM_U_HELPINDEX:
  152.         WinHelp( hwnd, "CHELP.HLP", HELP_INDEX, 0L );
  153.       break;
  154.       case CM_U_HELPHELP:
  155.         WinHelp( hwnd, "CHELP.HLP", HELP_HELPONHELP, 0L );
  156.       break;
  157.       case CM_U_HELPABOUT:
  158.          MessageBox( hwnd,"CHELP\n\nCopyright (c) 1992 Borland International",
  159.         "About CHELP", MB_ICONINFORMATION );
  160.       break;
  161.       case CM_EXIT:
  162.       if ( tfF1Pressed == TRUE )
  163.       {
  164.         WinHelp( hwnd, "CHELP.HLP", HELP_CONTEXT, HELP_EXIT );
  165.         tfF1Pressed = FALSE;
  166.       } else {
  167.         DestroyWindow( hwnd );
  168.       }
  169.       break;
  170.       }
  171.       break;
  172.  
  173.       case WM_DESTROY:
  174.          PostQuitMessage (0) ;
  175.          return 0 ;
  176.    }
  177.    return DefWindowProc (hwnd, message, wParam, lParam) ;
  178. }
  179.  
  180.